{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "1abb3568",
   "metadata": {},
   "source": [
    "----------------\n",
    "* 记录人：谢钰琳\n",
    "* 时间：2021年11月29日\n",
    "* week14\n",
    "* 主要参考文档：\n",
    "> 1.python官方文档\n",
    "> 2.博客--函数\n",
    "\n",
    "----------------"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "32e92a7d",
   "metadata": {},
   "source": [
    "# 本周内容\n",
    "\n",
    "> 1、函数的定义             \n",
    "> 2、如何使用函数          \n",
    "> 3、函数的参数信息               \n",
    "> 4、使用注释改进文档            \n",
    "> 5、传递任意数量的实参            \n",
    "> 6、函数与模块               "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ac3f5e2e",
   "metadata": {},
   "source": [
    "## 1、函数的定义\n",
    "\n",
    "-----\n",
    "\n",
    "\"\"\"\n",
    "def:定义函数的关键字\n",
    "flb：函数名\n",
    "(n)：定义函数的参数\n",
    "\"\"\"\"\"\"文档描述\n",
    "代码块：\n",
    "     a, b = 0, 1\n",
    "     while a < n:\n",
    "        print(a, end=' ')\n",
    "         a, b = b, a+b\n",
    "     print()\n",
    "\"\"\"\n",
    "\n",
    "-----\n",
    " \n",
    "* 定义函数:\n",
    "> 1.写def:   def定义函数的第一步              \n",
    "> 2.写定义函数名称: def function_name             \n",
    ">> 1.function_name 遵循变量名称定义的规则（只能用字母数字下划线作为起始）             \n",
    "> 3.函数名称后加\"()\" ; def function_name() ==> f(x) =2x\n",
    "\n",
    "* 书写函数体\n",
    "> 1.函数的注释\n",
    "> 2.书写函数具体操作（函数体）\n",
    "> 3.函数也是需要tab 的间距\n",
    "\n",
    "* 使用函数\n",
    "> 1.function_name()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "6662387e",
   "metadata": {},
   "outputs": [],
   "source": [
    "def fib(n):    # write Fibonacci series up to n\n",
    "    \"\"\"Print a Fibonacci series up to n.\"\"\"\n",
    "    a,b = 0,1\n",
    "    while a < n:\n",
    "        print(a, end=' ')\n",
    "        a, b = b, a+b\n",
    "    print()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "42ca4967",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 1 1 2 3 5 8 \n"
     ]
    }
   ],
   "source": [
    "fib(10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "d55e1eee",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on function fib in module __main__:\n",
      "\n",
      "fib(n)\n",
      "    Print a Fibonacci series up to n.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(fib)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "21e44c25",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 写一个实现hello world 的函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "0a7ab953",
   "metadata": {},
   "outputs": [],
   "source": [
    "def hello():   #定义一个函数,关键词为\"def\",函数名为\"greet_user\",最后以“：”结尾\n",
    "    \"打印hello world\"    #描述函数的具体功能\n",
    "    print(\"hello world!\")     #函数体的代码块，用于实现函数功能"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "7e9babf6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello world\n"
     ]
    }
   ],
   "source": [
    "hello()   #调用函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d146a6e1",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 函数的意义？"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "ea61684b",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 定义了一个名字叫“search4vowels”的函数\n",
    "def search4vowels(word):\n",
    "    \"\"\"\n",
    "    打印文本中出现的元音字母\n",
    "    请输入你想查询的文本内容\n",
    "    \"\"\"\n",
    "    vowels = set('aeiou')\n",
    "    found = vowels.intersection(set(word)) #交集运算\n",
    "    print(found)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "8e5d399e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on function search4vowels in module __main__:\n",
      "\n",
      "search4vowels(word)\n",
      "    打印文本中出现的元音字母\n",
      "    请输入你想查询的文本内容\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(search4vowels)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "738bdff1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'a', 'i'}\n"
     ]
    }
   ],
   "source": [
    "search4vowels(\"ahidknai\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "8262014b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'e'}\n"
     ]
    }
   ],
   "source": [
    "search4vowels(\"bgfhje\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fd36da52",
   "metadata": {},
   "outputs": [],
   "source": [
    "-----\n",
    "* 下面的方法是不是太重复了，主要原因是没有对重复功能的代码块有特殊的定义"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "b4f36bf3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Provide a word to search for vowels:dsgrtwgaaa\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'a'}"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "vowels = set('aeiou')\n",
    "word = input(\"Provide a word to search for vowels:\")\n",
    "found = vowels.intersection(set(word))\n",
    "found"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "adfb30f0",
   "metadata": {},
   "source": [
    "## 3、函数的参数信息\n",
    "* <font style=\"color:red\"> 注意：函数的参数可以没有，如果有参数，则如下(分为实际参数和形式参数） ：</font>\n",
    "\n",
    "> 1.形式参数（形参）\n",
    ">> 1.在定义过程中，函数的参数为形式参数\n",
    "> 2.实际参数（实参）\n",
    ">> 1.在调用过程中，函数的参数为实际参数\n",
    ">> 2.调用函数时，形式参数被分配空间，成为实际参数并应用起来"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "c06931db",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello world\n"
     ]
    }
   ],
   "source": [
    "# 函数可以没有参数\n",
    "hello()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "5934484f",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 定义函数\n",
    "def greet_user(username):    \n",
    "    \"\"\"显示简单的问候语\"\"\"    \n",
    "    print(\"hello,\"+username.title()+\"!\")    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "9199c3ae",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello,Wkkk!\n"
     ]
    }
   ],
   "source": [
    "# 调用函数\n",
    "greet_user(\"wkkk\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dde2b925",
   "metadata": {},
   "source": [
    "* 当参数不止一个：\n",
    "> 1.位置参数\n",
    ">> 1.形式参数有多个值，如果实际参数直接赋值，要按照形式参数的先后顺序，这种方式称：位置参数赋值                  \n",
    ">> 2.在调用函数过程中需要考量        \n",
    ">>> 1.位置          \n",
    ">>> 2.个数       \n",
    "\n",
    "> 2.关键字参数       \n",
    ">> 1.在调用函数时使用，我们直接在实参中用赋值的方式去定义形参         \n",
    ">> 2.不需要考虑顺序，因为赋值已经确定了形式参数的值  \n",
    "\n",
    "> 3.默认参数          \n",
    ">> 1.写在形参中            \n",
    ">> 2.可以省略赋值               "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "9b14b33d",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 请注意，有两个参数\n",
    "\n",
    "def describe_pet(animal_type,pet_name):\n",
    "    \"\"\"显示宠物信息\"\"\"\n",
    "    print(\"\\nI have a\" + animal_type + \".\")\n",
    "    print(\"My\" + animal_type + \"'s name is \"+pet_name.title() + \".\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "3ef3132b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "I have adog.\n",
      "Mydog's name is Harry.\n",
      "\n",
      "I have acat.\n",
      "Mycat's name is Python.\n"
     ]
    }
   ],
   "source": [
    "describe_pet('dog','harry')\n",
    "describe_pet('cat','python')  #函数调用多次是一种效率极高的工作方式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "id": "8802aab8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "I have adog.\n",
      "Mydog's name is Harry.\n"
     ]
    }
   ],
   "source": [
    "# 2.关键字参数  \n",
    "\n",
    "describe_pet(pet_name=\"harry\",animal_type=\"dog\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "id": "58c8e5ae",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 3.默认参数  \n",
    "\n",
    "def describe_pet(pet_name,animal_type=\"dog\",):\n",
    "    \"\"\"显示宠物信息\"\"\"\n",
    "    print(\"\\nI have a \" + animal_type + \".\")\n",
    "    print(\"My\" + animal_type + \"'s name is \"+pet_name.title() + \".\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "id": "4699bfc3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "I have a dog.\n",
      "Mydog's name is Hiiiii.\n"
     ]
    }
   ],
   "source": [
    "describe_pet(\"hiiiii\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b148393d",
   "metadata": {},
   "source": [
    "* 实操和注意\n",
    "\n",
    "> 1.当关键字参数和位置参数一起使用，关键字参数放在末尾   #eg2 #eg1          \n",
    "> 2.默认参数要放在末尾   #eg3         "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "id": "cf85c9a3",
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "positional argument follows keyword argument (<ipython-input-63-20571e065812>, line 6)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  File \u001b[1;32m\"<ipython-input-63-20571e065812>\"\u001b[1;36m, line \u001b[1;32m6\u001b[0m\n\u001b[1;33m    func_test(x=1,2)      #？\u001b[0m\n\u001b[1;37m                  ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m positional argument follows keyword argument\n"
     ]
    }
   ],
   "source": [
    "#eg1\n",
    "def func_test(x,y):\n",
    "    print(x)\n",
    "    print(y)\n",
    "    \n",
    "func_test(x=1,2)      #？\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "id": "4c1eb269",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "2\n"
     ]
    }
   ],
   "source": [
    "\n",
    "#eg2\n",
    "def func_test(x,y):\n",
    "    print(x)\n",
    "    print(y)\n",
    "    \n",
    "func_test(1,y=2)      \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "id": "e76331f2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "2\n"
     ]
    }
   ],
   "source": [
    "#eg3\n",
    "def func_test(x,y,z=2):\n",
    "    print(x)\n",
    "    print(y)\n",
    "    \n",
    "func_test(1,2)      #？"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7023ab4e",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
